﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.Data;

namespace KPIS.GERP.GIMS.APP.APP_UI.Add_Form
{
    public partial class ICM_Master_Contact : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        CommandControl cmdFac = new CommandControl();
        IBLLMasterContact bll = new MasterContactBLL();
        IBLLSysContactGroup bllGroup = new SysContactGroupBLL();
        IBLLSysContactStatus bllStatus = new SysContactStatusBLL();
        IBLLSysVat bllVat = new SysVatBLL();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadContactGroup();
                LoadContactStatus();
                LoadVat();
                LoadGridBank();
                LoadGridPartner();
                CalendarDate.FocusedDate = DateTime.Today;
                CalendarDate.SelectedDate = DateTime.Today;
            }
        }

        public void LoadContactGroup()
        {
            IList<SysContactGroupInfo> info = bllGroup.GetSysContactGroup();
            cbGroup.DataSource = info;
            cbGroup.DataBind();
        }

        public void LoadContactStatus()
        {
            IList<SysContactStatusInfo> info = bllStatus.GetSysContactStatus();
            rblStatus.DataSource = info;
            rblStatus.DataBind();
        }

        public void LoadVat()
        {
            IList<SysVatInfo> info = bllVat.GetSysVat();
            cbVat.DataSource = info;
            cbVat.DataBind();
        }

        public void LoadGridBank()
        {
            IList<MasterContactBankInfo> infoListBank = new List<MasterContactBankInfo>();
            if (ViewState["Bank"] != null)
            {
                infoListBank = (List<MasterContactBankInfo>)ViewState["Bank"];
            }
            Grid_Bank.DataSource = infoListBank;
            Grid_Bank.DataBind();

            cmdFac.GridCommand(panelList_Bank, panelGrid_Bank, panelCommand_Bank);
        }

        public void LoadGridPartner()
        {
            IList<MasterContactPartnerInfo> infoListPartner = new List<MasterContactPartnerInfo>();
            if (ViewState["Partner"] != null)
            {
                infoListPartner = (List<MasterContactPartnerInfo>)ViewState["Partner"];
            }
            Grid_Partner.DataSource = infoListPartner;
            Grid_Partner.DataBind();

            cmdFac.GridCommand(panelList_Partner, panelGrid_Partner, panelCommand_Partner);
        }

        protected void AddNew_Bank_Click(object sender, EventArgs e)
        {
            cmdFac.AddNewCommand(panelList_Bank, panelGrid_Bank, panelCommand_Bank);
            RequiredFieldValidator valCode = new RequiredFieldValidator();
            valCode.ID = "ValtxtBankCode";
            valCode.ControlToValidate = "txtBankCode";
            valCode.SetFocusOnError = true;
            valCode.Display = ValidatorDisplay.Dynamic;
            valCode.ErrorMessage = "* กรุณาระบุรหัสธนาคาร *";
            panelValtxtBankCode.Controls.Add(valCode);
            RequiredFieldValidator valName = new RequiredFieldValidator();
            valName.ID = "ValtxtBankName";
            valName.ControlToValidate = "txtBankName";
            valName.SetFocusOnError = true;
            valName.Display = ValidatorDisplay.Dynamic;
            valName.ErrorMessage = "* กรุณาระบุชื่อธนาคาร *";
            panelValtxtBankName.Controls.Add(valName);
        }

        protected void AddNewSave_Bank_Click(object sender, EventArgs e)
        {
            IList<MasterContactBankInfo> infoListBank = new List<MasterContactBankInfo>();
            MasterContactBankInfo info = new MasterContactBankInfo();
            RecordInfo infoRecord = new RecordInfo();

            if (ViewState["Bank"] != null)
            {
                infoListBank = (List<MasterContactBankInfo>)ViewState["Bank"];
            }
            info.Code = txtBankCode.Text;
            info.Name = txtBankName.Text;
            info.Branch = txtBankBranch.Text;
            info.Account_code = txtAccountCode.Text;
            info.Account_name = txtAccountName.Text;
            infoRecord.Created_by = cn.UserID;
            infoRecord.Created_when = DateTime.Now;
            info.InfoRecord = infoRecord;
            infoListBank.Add(info);
            ViewState["Bank"] = infoListBank;
            LoadGridBank();
        }

        protected void Delete_Bank_Click(object sender, EventArgs e)
        {
            int[] data_value = new int[Grid_Bank.MasterTableView.GetSelectedItems().Length];
            int item = Grid_Bank.MasterTableView.GetSelectedItems().Length;
            foreach (GridDataItem data in Grid_Bank.MasterTableView.GetSelectedItems())
            {
                item = item - 1;
                data_value[item] = data.ItemIndex;
            }

            for (int i = 0; i < data_value.Length; i++)
            {
                ((List<MasterContactBankInfo>)ViewState["Bank"]).RemoveAt(data_value[i]);
            }

            LoadGridBank();
        }

        protected void ShowGrid_Bank_Click(object sender, EventArgs e)
        {
            LoadGridBank();
        }

        protected void Grid_Bank_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridBank();
        }

        protected void AddNew_Partner_Click(object sender, EventArgs e)
        {
            cmdFac.AddNewCommand(panelList_Partner, panelGrid_Partner, panelCommand_Partner);
            RequiredFieldValidator valName = new RequiredFieldValidator();
            valName.ID = "ValPartnerName";
            valName.ControlToValidate = "txtPartnerName";
            valName.SetFocusOnError = true;
            valName.Display = ValidatorDisplay.Dynamic;
            valName.ErrorMessage = "* กรุณาระบุชื่อ-สกุลผู้ถือหุ้น *";
            panelValPartnerName.Controls.Add(valName);
            RequiredFieldValidator valCitizen = new RequiredFieldValidator();
            valCitizen.ID = "ValPartnerCitizen";
            valCitizen.ControlToValidate = "txtPartnerCitizen";
            valCitizen.SetFocusOnError = true;
            valCitizen.Display = ValidatorDisplay.Dynamic;
            valCitizen.ErrorMessage = "* กรุณาระบุหมายเลขบัตรประชาชน *";
            panelValPartnerCitizen.Controls.Add(valCitizen);
            CustomValidator valCheck = new CustomValidator();
            valCheck.ID = "ValCheckCitizen";
            valCheck.ControlToValidate = "txtPartnerCitizen";
            valCheck.SetFocusOnError = true;
            valCheck.Display = ValidatorDisplay.Dynamic;
            valCheck.ErrorMessage = "* หมายเลขบัตรประชาชนไม่ถูกต้อง *";
            valCheck.ClientValidationFunction = "ValidateCitizen";
            panelValPartnerCitizen.Controls.Add(valCheck);
        }

        protected void AddNewSave_Partner_Click(object sender, EventArgs e)
        {
            IList<MasterContactPartnerInfo> infoListPartner = new List<MasterContactPartnerInfo>();
            MasterContactPartnerInfo info = new MasterContactPartnerInfo();
            RecordInfo infoRecord = new RecordInfo();

            if (ViewState["Partner"] != null)
            {
                infoListPartner = (List<MasterContactPartnerInfo>)ViewState["Partner"];
            }
            info.Name = txtPartnerName.Text;
            info.Citizen = txtPartnerCitizen.Text;
            info.Descr = txtPartnerDescr.Text;
            infoRecord.Created_by = cn.UserID;
            infoRecord.Created_when = DateTime.Now;
            info.InfoRecord = infoRecord;
            infoListPartner.Add(info);
            ViewState["Partner"] = infoListPartner;
            LoadGridPartner();
        }

        protected void Delete_Partner_Click(object sender, EventArgs e)
        {
            int[] data_value = new int[Grid_Partner.MasterTableView.GetSelectedItems().Length];
            int item = Grid_Partner.MasterTableView.GetSelectedItems().Length;
            foreach (GridDataItem data in Grid_Partner.MasterTableView.GetSelectedItems())
            {
                item = item - 1;
                data_value[item] = data.ItemIndex;
            }

            for (int i = 0; i < data_value.Length; i++)
            {
                ((List<MasterContactPartnerInfo>)ViewState["Partner"]).RemoveAt(data_value[i]);
            }

            LoadGridPartner();
        }

        protected void ShowGrid_Partner_Click(object sender, EventArgs e)
        {
            LoadGridPartner();
        }

        protected void Grid_Partner_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGridPartner();
        }

        protected void finishButton_Partner_Click(object sender, EventArgs e)
        {
            MasterContactInfo info = new MasterContactInfo();
            SysContactGroupInfo infoGroup = new SysContactGroupInfo();
            SysContactStatusInfo infoStatus = new SysContactStatusInfo();
            SysVatInfo infoVat = new SysVatInfo();
            RecordInfo infoRecord = new RecordInfo();
            IList<MasterContactBankInfo> infoListBank = new List<MasterContactBankInfo>();
            IList<MasterContactPartnerInfo> infoListPartner = new List<MasterContactPartnerInfo>();

            if (ViewState["Bank"] != null)
            {
                infoListBank = (List<MasterContactBankInfo>)ViewState["Bank"];
            }
            if (ViewState["Partner"] != null)
            {
                infoListPartner = (List<MasterContactPartnerInfo>)ViewState["Partner"];
            }

            infoGroup.Seq = Convert.ToInt32(cbGroup.SelectedValue);
            info.Name_th = txtNameTh.Text;
            info.Name_eng = txtNameEn.Text;
            info.Coordinator = txtCoordinator.Text;
            info.Gfmis_code = txtGfmis.Text;
            info.Tax_code = txtTax.Text;
            infoVat.Seq = Convert.ToInt32(cbVat.SelectedValue);
            info.Reg_capital = txtRegCapital.Value;
            if (txtDate.Text.Length > 0)
            {
                info.Reg_date = Convert.ToDateTime(txtDate.TextWithLiterals);
            }
            infoStatus.Seq = Convert.ToInt32(rblStatus.SelectedValue);
            info.Country = txtCountry.Text;
            info.Address = txtAddress.Text;
            info.Building = txtBuilding.Text;
            info.Floor = txtFloor.Text;
            info.Village = txtVillage.Text;
            info.Road = txtRoad.Text;
            info.Lane = txtLane.Text;
            info.District = txtDistrict.Text;
            info.Amphur = txtAmphur.Text;
            info.Post = txtPost.Text;
            info.Province = txtProvince.Text;
            info.Tel = txtTel.Text;
            info.Tel_next = txtTelNext.Text;
            info.Fax = txtFax.Text;
            info.Fax_next = txtFaxNext.Text;
            info.Email = txtEmail.Text;
            infoRecord.Created_by = cn.UserID;
            infoRecord.Created_when = DateTime.Now;
            info.InfoGroup = infoGroup;
            info.InfoStatus = infoStatus;
            info.InfoVat = infoVat;
            info.InfoRecord = infoRecord;

            try
            {
                bll.Insert(info, infoListBank, infoListPartner);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }

            RadManager1.Alert(SystemInfo._InsertComplete);
            RadManager1.ResponseScripts.Add(@"CloseWindow();");
        }

        protected void clearFilter_Bank_Click(object sender, EventArgs e)
        {
            foreach (GridColumn column in Grid_Bank.MasterTableView.Columns)
            {
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                column.CurrentFilterValue = string.Empty;
            }
            Grid_Bank.MasterTableView.FilterExpression = string.Empty;
            LoadGridBank();
        }

        protected void clearFilter_Partner_Click(object sender, EventArgs e)
        {
            foreach (GridColumn column in Grid_Partner.MasterTableView.Columns)
            {
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                column.CurrentFilterValue = string.Empty;
            }
            Grid_Partner.MasterTableView.FilterExpression = string.Empty;
            LoadGridPartner();
        }
    }
}
